//
//  BBHelp.swift
//  BBLinkiOSProject
//
//  Created by liuzhu on 2018/12/29.
//  Copyright © 2018 liuzhu. All rights reserved.
//

import UIKit

class BBHelp: NSObject {
    @objc var userId:String?
    @objc var deviceId:String?
    @objc var serverId:String?
    @objc var helpId:String?
    @objc var helpImages:String?
    @objc var deviceCheckInfo:String?
    @objc var addressDetail:String?
    @objc var latitude:String?
    @objc var longitude:String?
    @objc var helpDes:String?
    @objc var status:NSNumber?
    @objc var createTime:NSNumber?
    @objc var updateTime:NSNumber?
    
    //字典转模型
    init(dict:[String:AnyObject]){
        super.init()
        setValuesForKeys(dict)
    }
    
    override func setValue(_ value: Any?, forUndefinedKey key: String) {
        print(key)
    }
    
    class func loadData(finish:@escaping (_ model:[BBHelp]?,_ error:Error?)->()){
        let path = "help/list.do"
        let params = ["token":BBUser.loadUser()?.token, "userId":BBUser.loadUser()?.userId]
        AFNetWorkManager.shareNetworkTools().post(path, parameters: params, success: { (_, JSON) in
            let dict = JSON as![String:AnyObject]
            let dictStatus = dict["status"]as!NSNumber
            if(dictStatus == 0){
                let data = dict["data"]
                let models = dict2Models(list: data!["list"] as! [[String:AnyObject]])
                finish(models,nil)
            }
            else{
                finish(nil,nil)
            }
        }) { (_, error) in
            finish(nil,error)
        }
    }
    
    //将字段数组转换为模型数组
    class func dict2Models(list:[[String:AnyObject]])->[BBHelp]{
        var models = [BBHelp]()
        for dict in list{
            models.append(BBHelp(dict: dict))
        }
        return models
    }
    
    //将字段数组转换为模型数组
    class func dict2Model(dict:[String:AnyObject])->BBHelp{
        return BBHelp(dict: dict)
    }
}
